package com.example.administrator.smartdevice.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.smartdevice.R;
import com.example.administrator.smartdevice.activity.DeviceDetailsActivity;
import com.example.administrator.smartdevice.adapter.DeviceAdapter;
import com.example.administrator.smartdevice.base.BaseFragment;
import com.example.administrator.smartdevice.model.ApplianceModel;
import com.example.administrator.smartdevice.net.HttpUtils;
import com.example.administrator.smartdevice.utils.AgentConstants;
import com.example.administrator.smartdevice.utils.AgentSharedPreferences;
import com.example.administrator.smartdevice.utils.ServerConstants;
import com.example.administrator.smartdevice.widget.CustomTipDialog;
import com.hhc.netty.DeviceModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/4/30.
 */

public class DeviceListFragment extends BaseFragment {
    @BindView(R.id.fragment_device_list)
    protected RecyclerView fragment_device_list;

    private DeviceAdapter deviceAdapter;
    private List<ApplianceModel> data = new ArrayList<>();

    @BindView(R.id.fragment_message_refreshLayout)
    protected SmartRefreshLayout fragment_message_refreshLayout;

    public static DeviceListFragment newInstance() {
        return new DeviceListFragment();
    }

    @Override
    protected void initData(View view, Bundle savedInstanceState) {
        initView();
        initRefresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_device_list;
    }

    @Override
    protected void onFailure(int requestCode, Object object) {
        stopTipDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object) {
        stopTipDialog();
        fragment_message_refreshLayout.finishRefresh();
        if (object != null) {
            switch (requestCode) {
                case 1001:
                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                        showToast(HttpUtils.getResultMessage(object));
                        getApplianceList();
                    } else {
                        showToast(HttpUtils.getResultMessage(object));
                    }
                    break;
                case 1002:
                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                        ApplianceModel[] deviceModels = (ApplianceModel[]) HttpUtils.getData(object, ApplianceModel[].class);
                        if (deviceModels != null) {
                            data = Arrays.asList(deviceModels);
                            deviceAdapter.setNewData(data);
                        }
                    } else {
                        showToast(HttpUtils.getResultMessage(object));
                    }
                    break;
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getApplianceList();
    }

    protected void getApplianceList() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("memberId", AgentSharedPreferences.getLastLoginId(mBaseActivity));
        jsonObject.put("type", "10");
        HttpUtils.post(mBaseActivity, 1002, ServerConstants.QUERY_APPLIANCE_LIST, jsonObject.toString(), this);
        startTipDialog(CustomTipDialog.ICON_TYPE_LOADING, getString(R.string.common_loading), true);
    }

    protected void initView() {
        fragment_device_list.setLayoutManager(new GridLayoutManager(mBaseActivity, 3));
        deviceAdapter = new DeviceAdapter(R.layout.activity_device_item_device, data);
        deviceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("masterId", data.get(position).getMasterId());
                bundle.putString("applianceName", data.get(position).getApplianceName());
                startOtherActivity(DeviceDetailsActivity.class, bundle);
            }
        });
        fragment_device_list.setAdapter(deviceAdapter);
    }

    public void initRefresh() {
        fragment_message_refreshLayout.setEnableLoadmore(false);
        fragment_message_refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getApplianceList();
            }
        });
    }
}
